#include "hitbox.h"

Hitbox::Hitbox(int x, int y, int w, int h)
{
	xy = new Vector((float)x, (float)y);
	wh = new Vector((float)w, (float)h);
	for(int i=0; i<4; i++)
		worldCoords[i] = 0;
	identifier = "unset";
}
Hitbox::Hitbox()
{
	xy = 0;
	wh = 0;
	for(int i=0; i<4; i++)
		worldCoords[i] = 0;
}
Hitbox::~Hitbox()
{
	if(xy != 0)
		delete xy;
	if(wh != 0)
		delete wh;
}
void Hitbox::setIdentifier(char *str)
{
	identifier = str;
}
void Hitbox::setDimensions(int x, int y, int w, int h)
{
	if(xy != 0)
		delete xy;
	if(wh != 0)
		delete wh;

	xy = new Vector((float)x, (float)y);
	wh = new Vector((float)w, (float)h);
}
void Hitbox::calcWorldCoords(int xx, int yy, int w, int h, float angle)
{
	if(worldCoords[0] != 0)
		clearWorldCoords();
	
	float cs = cos(angle);
	float sn = sin(angle);
	Vector *origin = new Vector(xx+(w/2), yy+(h/2));
	float x = (xy->getX()+xx)+wh->getX();
	float y = (xy->getY()+yy)-wh->getY();
	x -= origin->getX();
	y -= origin->getY();
	float tmpx = (x*cs - y*sn);
	float tmpy = (x*sn + y*cs);
	tmpx += origin->getX();
	tmpy += origin->getY();
	worldCoords[0] = new Vector(tmpx, tmpy);

	x -= (wh->getX()*2);
        tmpx = (x*cs - y*sn);
	tmpy = (x*sn + y*cs);
        tmpx += origin->getX();
	tmpy += origin->getY();
	worldCoords[1] = new Vector(tmpx, tmpy);

	y += (wh->getY()*2);
	tmpx = (x*cs - y*sn);
        tmpy = (x*sn + y*cs);
        tmpx += origin->getX();
        tmpy += origin->getY();
	worldCoords[2] = new Vector(tmpx, tmpy);

	x += (wh->getX()*2);
	tmpx = (x*cs - y*sn);
        tmpy = (x*sn + y*cs);
        tmpx += origin->getX();
        tmpy += origin->getY();
	worldCoords[3] = new Vector(tmpx, tmpy);

	delete origin;

	/*
	worldCoords[0] = new Vector(xoffset+(cs * -wh->getX() - sn * -wh->getY()+xy->getX()) ,yoffset+(sn * -wh->getX() + cs * -wh->getY()+xy->getY()));
	worldCoords[1] = new Vector(xoffset+(cs * -wh->getX() - sn * wh->getY()+xy->getX()),yoffset+(sn * -wh->getX() + cs * wh->getY()+xy->getY()));
	worldCoords[2] = new Vector(xoffset+(cs * wh->getX() - sn * wh->getY()+xy->getX()),yoffset+(sn * wh->getX() + cs * wh->getY()+xy->getY()));
	worldCoords[3] = new Vector(xoffset+(cs * wh->getX() - sn * -wh->getY()+xy->getX()),yoffset+(sn * wh->getX() + cs * -wh->getY()+xy->getY()));
	*/
}
void Hitbox::clearWorldCoords()
{
	for(int i=0; i<4; i++)
		delete worldCoords[i];
}
Vector *Hitbox::getWorldCoord(int i)
{
	return worldCoords[i];
}
